/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){
       loaddata("newmessage","newmessagelist",0);
       loaddata("backmessagelist","backmessage",0);
});

function loaddata(u,parentid,page){
    $.ajax({
           url:url+"/control/message/"+u+"/"+page+".html",
           dataType:"json",
           error:function(){
               $("#"+parentid).html("请求失败内容调用出错!");
           },
           success:function(data){
              if(data.error)
                   $("#"+parentid).html("内容调用出错!");
            else{
                var result="";
                for(var x in data.data){
                    result+="<div class=\"accordion-group\" rel=\""+data.data[x]["id"]+"\">\
                                            <div class=\"accordion-heading\">\
                                                <a class=\"accordion-toggle\" data-toggle=\"collapse\" data-parent=\"#"+parentid+"\" href=\"#messagebox"+x+"\">"+getLocalTime(data.data[x]["uptime"])+"</a>\
                                            </div>\
                                            <div id=\"messagebox"+x+"\" class=\"accordion-body collapse\">\
                                                <div class=\"accordion-inner\">"+data.data[x]["message"]+"</div>\
                                                <div style=\"padding:10px;\">\
                                                    <textarea rows=\"3\" class=\"span12\" style=\"margin-top:10px;\" placeholder=\"回复内容\">"+data.data[x].back+"</textarea>\
                                                    <button id=\"postbtn\" class=\"btn btn-success pull-right\" type=\"submit\"  style=\"margin-right:10px; margin-bottom:10px;\"><i class=\"icon-ok\"></i> 回复</button>\
                                                </div>\
                                            </div>\
                                        </div>";
                }
                 $("#"+parentid).html(result);
            }
            opend(parentid);
        },
        complete:function(){
            $("#"+parentid).nextAll("#load").addClass("hidden");
            $("#"+parentid).addClass("show").removeClass("hidden");
        }
    });
}

function opend(parentid){
    $("#"+parentid+" .accordion-group").click(function(){
        var mid=$(this).attr("rel");
        $("[id=postbtn]").click(function(){
            $(this).attr("disabled", true);
            var info=$(this).prev().val();
            $.post(url+"/control/message/backmessage.html",{id:mid,back:info},function(data){
                if(data.error)
                    alert("回复成功！");
                else
                    alert("回复失败！");
            },"json");
        });
    });
}